<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
    <meta charset="UTF-8">
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" href="./stylesheets/style.css">
    <!-- Angular-->
<script src="http://cdn.bootcss.com/angular.js/1.4.0-rc.1/angular.min.js"></script>
    <title>Snack Bar</title>
    <style type="text/css">
        ul{list-style:none;}
    </style>
</head>
<body ng-controller="myCtrl">
  <div class="navbar-wrapper">
      <div class="container">

        <nav class="navbar navbar-inverse navbar-fixed-top">
          <div class="container">
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="#" ng-click = "getAll();">Snack Bar</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
              <ul class="nav navbar-nav">
                <li><a href="/" ng-click = "getAll();">首页</a></li>
                <li><a href="/register">注册</a></li>
                <li><a href="/login">登录</a></li>
              </ul>
            </div>
          </div>
        </nav>

      </div>
  </div>
  <div class="snackcontainer">
  <form class="form-signin" method="post" ng-submit="processForm()">
     <legend>用户注册</legend>
     <div class="form-group">
        <label class="control-label" for="username" >用户名</label>
        <input type="text" class="form-control" id="username" name="username" ng-model="formData.name">
         <p class="help-block">你的账户名称，用于登录和显示。</p>
     </div>
     <div class="form-group">
         <label class="control-label" for="password">密码</label>
         <input type="password" class="form-control" id="password" name="password" ng-model="formData.password">
     </div>
     <div class="form-group">
         <label class="control-label" for="password-repeat">重复输入密码</label>
         <input type="password" class="form-control" id="password-repeat" name="password-repeat" ng-model="formData.repassword">
     </div>
         <button type="submit" ng-submit="processForm()" class="btn btn-lg btn-primary btn-block">注册</button>
         <br/>
         <div ng-class="{true:'alert alert-danger',false:'alert alert-success'}[isFail]" ng-show="isFail" role="alert">{{ registerMessage }}</div>
   </form>
   
 </div>
<script>
    var app=angular.module("app",[], function () {
        console.log('started');
    });
app.controller("myCtrl",function($scope,$http){
	 $scope.formData = {};
	$scope.processForm = function() {
     if (!$scope.formData.name||!$scope.formData.password) {
        $scope.registerMessage = "请输入用户名和密码!";
        $scope.isFail = true;
        return;
     }
		 if( $scope.formData.password != $scope.formData.repassword){
			 $scope.registerMessage = "两次输入的密码不一样!";
       $scope.isFail = true;
		 }else{
			 $http({
			        method  : 'get',
			        url     : './user/user/' + $scope.formData.name,
			       // data    : $.param($scope.formData),  // pass in data as strings
			        headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
			    })
			        .success(function(data) {
			            console.log(data);
			            if (data.message) {
			            	 $scope.registerMessage = "该名字已经被使用";
                     $scope.isFail = true;
			            } else {
						      if($scope.formData.password){
			          	             $scope.formData.password = hex_md5($scope.formData.password);
			                   // alert($scope.formData.password);
			                  }
			            	 $http({
			 			        method  : 'POST',
			 			        url     : '/register',
			 			        data    : {
                                name:$scope.formData.name,
                                password:$scope.formData.password,
                                type:'Normal'
                              },  // pass in data as strings
			 			    })
			 			        .success(function(data) {
			 			            console.log(data);
			 			            if (!data.success) {
			 			            	$scope.registerMessage = "注册失败";
                          $scope.isFail = true;
			 			            } else {
			 			            	$scope.registerMessage = "注册成功";
                          $scope.isFail = false;
										location.href = './';
			 			            	//res.redirect('./');
			 			            }
			 			        });
			            }
			        });
		 }
		// alert( $scope.formData.password);
		// $scope.registerMessage = 'sucess!';
    };
});
</script>

<!-- FOOTER -->
<footer class="footer">
  <p>&copy; 2015 Company, Inc. &middot; Powered By &middot; <a href="https://nodejs.org/">Nodejs</a></p>
</footer>
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="../util/md5.js"></script>
</body>
</html>